package com.xhwl.centiotdata.suppliersystem.younijia;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.xhwl.centiotdata.pojo.constant.YouNiJiaConstant;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.younijia.DeviceYouNiJiaDTO;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import com.xhwl.common.pojo.cent.device.Device;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author msy
 */
@Slf4j
@Service
public class DeviceYouNiJiaHandler {

    /**
     * 拉取人行设备
     * @param configDTO
     * @return
     */
    public List<DeviceYouNiJiaDTO> pullPedestrianDevice(DeviceIntegrationConfigDTO configDTO){
        String host = configDTO.getIpAndPort();
        String url = host + YouNiJiaConstant.DEVICE_LIST;
        String token = configDTO.getToken();

        List<DeviceYouNiJiaDTO> deviceYouNiJiaDTOList = new ArrayList<>();
        Integer pageNum = YouNiJiaConstant.PAGE_NUM;
        Integer pageSize = YouNiJiaConstant.PAGE_SIZE;
        Integer total;
        do {
            Map<String, Object> params = new HashMap<>();
            params.put("service_id", configDTO.getServiceId());
            params.put("pageNum", pageNum);
            params.put("pageSize", pageSize);
            params.put("token", token);

            String str = HttpRequest.post(url).body(JSON.toJSONString(params)).execute().body();
            JSONObject result = new JSONObject(str);
            JSONArray data = result.getJSONArray("data");

            if(data == null || data.isEmpty()) {
                return new ArrayList<>();
            }
            total = result.getInt("Total");
            List<DeviceYouNiJiaDTO> add = JSON.parseArray(data.toString(), DeviceYouNiJiaDTO.class);
            deviceYouNiJiaDTOList.addAll(add);
        } while ((pageSize * pageNum++) < total);
        return deviceYouNiJiaDTOList;
    }

    public List<DeviceDTO> handlePedestrianDevice(DeviceIntegrationConfigDTO configDTO, List<DeviceYouNiJiaDTO> deviceYouNiJiaDTOList) {
        List<DeviceDTO> deviceDTOList = new ArrayList<>();
        for(DeviceYouNiJiaDTO item : deviceYouNiJiaDTOList) {
            DeviceDTO deviceDTO = new DeviceDTO();
            deviceDTO.setProjectId(configDTO.getProjectId());
            deviceDTO.setProductId(configDTO.getProductId());
            deviceDTO.setThirdDeviceId(item.getEquipment_id());
            deviceDTO.setName(item.getDoor_name());
            deviceDTO.setCategoryId(configDTO.getCategoryEnum().getId());
            deviceDTO.setDeviceTypeId(configDTO.getDeviceType());
            deviceDTO.setSupplierId(97);
            deviceDTO.setOnlineStatus("1000".equals(item.getStatus_cd()) ? 1 : 0);
            deviceDTOList.add(deviceDTO);
        }
        return deviceDTOList;
    }

    public List<Device> handlePedestrianDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO, List<DeviceYouNiJiaDTO> deviceYouNiJiaDTOList) {
        List<Device> deviceList = new ArrayList<>();
        for(DeviceYouNiJiaDTO item : deviceYouNiJiaDTOList) {
            Device device = new Device();
            device.setProjectId(configDTO.getProjectId());
            device.setThirdDeviceId(item.getEquipment_id());
            device.setDeviceTypeId(configDTO.getDeviceType());
            device.setOnlineStatus("1000".equals(item.getStatus_cd()) ? DeviceOnlineStatusEnum.ONLINE : DeviceOnlineStatusEnum.OFFLINE);
            deviceList.add(device);
        }
        return deviceList;
    }
}
